﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmItem : Form
    {
        private List<string> GvCategory = new List<string>();
        private List<string> GvSubcategory = new List<string>();

        public frmItem()
        {
            InitializeComponent();
            gbxpvbvdisprice.Visible = false;


        }
        //Create by Piumi

        #region ADD ITEM
        #region Save
        private void btnItemSave_Click(object sender, EventArgs e)
        {
            if (txtBarcode.Text != null)
            {
                int result = SaveButton();
                if (result != 0)
                {
                    MessageBox.Show("ITEM WAS SUCCESFULLY SAVED");
                }
            }
        }

        private Product CollectItemFigures()
        {
            try
            {
                Product LvItem = new Product();
                LvItem.Brand_Name = txtItemBrand.Text;
                LvItem.Cat_ID = cmdItemcat.Text;
                LvItem.Date_Create = System.DateTime.Now;
                LvItem.Description = rtxtDescription.Text;
                LvItem.Last_Modify = System.DateTime.Now;
                LvItem.Pro_ID = txtItemCode.Text;
                LvItem.Pro_Name = txtItem.Text;
                LvItem.Status = cmbStatus.Text;
                LvItem.Subcat_ID = cmdSubCat.Text;
                LvItem.Unit = Convert.ToDecimal(cmbUnit.Text);
                LvItem.Unit_Description = txtUnitDescription.Text;
                LvItem.User_Id = "1";
                LvItem.User_Modify = "1";
                LvItem.BarCode = txtBarcode.Text;
                return LvItem;

            }
            catch (Exception)
            {
                return null;
            }
        }

        private Price_List CollectPriceListFigures()
        {
            try
            {
                Price_List LvItemList = new Price_List();
                LvItemList.BV = Convert.ToDecimal(txtBV.Text);
                LvItemList.Date_Create = System.DateTime.Now;
                LvItemList.Date_Modify = System.DateTime.Now;
                LvItemList.Exp_Date = Convert.ToDateTime(dtpExpdate.Text);
                LvItemList.PL_Id = Price_ListDAO.GetNewPriceListID();
                LvItemList.Pro_ID = txtItem.Text;
                LvItemList.Pro_Price = Convert.ToDecimal(txtDisPrice.Text);
                LvItemList.Pur_price = Convert.ToDecimal(txtCost.Text);
                LvItemList.PV = Convert.ToDecimal(txtPV.Text);
                LvItemList.Ret_Price = Convert.ToDecimal(txtRetailprice.Text);
                LvItemList.User_Id = "1";
                LvItemList.User_Modify = "1";
                return LvItemList;
            }
            catch (Exception)
            {
                return null;
            }

        }

        private int SaveButton()
        {
            Price_List LvItemPL = CollectPriceListFigures();
            Product LvItem = CollectItemFigures();
            if (LvItem != null && LvItemPL != null)
            {
                return ProductDAO.SaveItem(LvItem, LvItemPL);
            }
            else
            {
                return 0;
            }
        }

        #endregion

        #region New

        private void btnItemNew_Click(object sender, EventArgs e)
        {
            //Sub Category Combo box
            Sub_CategoryDAO.GetAllSubcategories();
            foreach (string LvSubcategory in GvSubcategory)
            {
                this.cmdSubCat.Items.Add(LvSubcategory);
            }

            //Category Combo Box
            CategoryDAO.GetAllCategories();
            foreach (string LvCategory in GvCategory)
            {
                this.cmdItemcat.Items.Add(LvCategory);
            }

            //Product ID
            this.txtItem.Text = ProductDAO.GetNewProduct();
        }
        #endregion

        #region Clear
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtItemBrand.Text = "";
            cmdItemcat.Text = "";
            rtxtDescription.Text = "";
            txtItemCode.Text = "";
            txtItem.Text = "";
            cmbStatus.Text = "";
            cmdSubCat.Text = "";
            cmbUnit.Text = "";
            txtUnitDescription.Text = "";
        }
        #endregion

        private void txtCost_TextChanged(object sender, EventArgs e)
        {
            if (txtRetailprice != null)
            {
                gbxpvbvdisprice.Visible = true;
                List<Price_List> LvPricelist = new List<Price_List>();
                LvPricelist = Price_ListDAO.CalculationItemDetails(Convert.ToDecimal(txtRetailprice.Text), Convert.ToDecimal(txtCost.Text));
                foreach (Price_List item in LvPricelist)
                {
                    txtPV.Text = item.PV.ToString();
                    txtBV.Text = item.BV.ToString();
                    txtDisPrice.Text = item.Pro_Price.ToString();
                }

            }
        }

        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            if (txtRetailprice != null)
            {
                gbxpvbvdisprice.Visible = true;
                List<Price_List> LvPricelist = new List<Price_List>();
                LvPricelist = Price_ListDAO.CalculationItemDetails(Convert.ToDecimal(txtRetailprice.Text), Convert.ToDecimal(txtCost.Text));
                foreach (Price_List item in LvPricelist)
                {
                    txtPV.Text = item.PV.ToString();
                    txtBV.Text = item.BV.ToString();
                    txtDisPrice.Text = item.Pro_Price.ToString();
                }

            }
        }




        private void FillGridExistingCategories()
        {
            //Fill the DataGrid
            List<Category> LvList = CategoryDAO.GetAllCategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            //dgvCategory.DataSource = LvTable;
            LvTable.Columns.Add("CategoryID ");
            LvTable.Columns.Add("CategoryName ");
            //LvTable.Columns.Add("Status ");
            //LvTable.Columns.Add("Description ");


            foreach (Category LvCategory in LvList)
            {


                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() + "-" + LvCategory.Cat_Name.ToString();

            }

            this.cmdItemcat.DisplayMember = "CategoryID ";
            this.cmdItemcat.DataSource = LvTable;

        }


        private void FillSubCatGrid()
        {
            //Fill the DataGrid
            List<Sub_Category> LvList = Sub_CategoryDAO.GetAllSubcategories();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;



            LvTable.Columns.Add("SubCategoryID ");

            LvTable.Columns.Add("SubCategoryName ");



            foreach (Sub_Category LvSubCategory in LvList)
            {
                //LvTable = (DataTable)dgvSubCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());


                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvSubCategory.Subcat_ID.ToString() + "-" + LvSubCategory.Cat_Name.ToString();


            }

            this.cmdSubCat.DisplayMember = "SubCategoryID ";
            this.cmdSubCat.DataSource = LvTable;

        }
        #endregion

        private void frmItem_Load(object sender, EventArgs e)
        {

        }

        private void btnPackageNew_Click(object sender, EventArgs e)
        {

        }

        //private void txtBarcode_TextChanged(object sender, EventArgs e)
        //{


        //}
    }
}
